package Realchat;

import javax.xml.crypto.Data;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;

public class Send implements  Runnable{
    DatagramSocket socket=null;
    BufferedReader reader=null;

    private int fromPort;
    private String toIP;
    private int toPort;

    public Send(int fromPort,String toIP,int toPort){
        this.fromPort=fromPort;
        this.toIP=toIP;
        this.toPort=toPort;

        try {
            socket=new DatagramSocket(fromPort);
            reader=new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void run() {
          while(true) {
              try {
                  {   String data = reader.readLine();
                      byte[] datas =data.getBytes();
                      DatagramPacket datagramPacket = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress(this.toIP, this.toPort));
                      socket.send(datagramPacket);
                      if (datas.equals("bye")) {
                          break;
                           }
                  }

              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
       socket.close();

    }

}
